package com.zone.vehicle.superApps.repository;

import android.content.Context;

import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.datasource.AppManagerDataSource;
import com.zone.vehicle.superApps.db.entity.App;
import com.zone.vehicle.superApps.enums.AppTypeEnum;
import com.zone.vehicle.superApps.utils.BeanUtils;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class AppManagerRepository {

    private static volatile AppManagerRepository instance;

    private AppManagerDataSource dataSource;


    // private constructor : singleton access
    private AppManagerRepository(AppManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public static AppManagerRepository getInstance(AppManagerDataSource dataSource) {
        if (instance == null) {
            instance = new AppManagerRepository(dataSource);
        }
        return instance;
    }

    public List<AppEntity> getActivityApp(AppTypeEnum type){
        List<AppEntity> appEntityList = new ArrayList<>();
        List<App> list = dataSource.getActivityApp(type);
        if (ObjectUtils.isNotEmpty(list)){
            for (App app : list) {
                AppEntity appEntity = new AppEntity();
                BeanUtils.copyProperties(app, appEntity);
                appEntityList.add(appEntity);
            }
        }
        return appEntityList;
    }

    public List<AppEntity> getInstalledApp(AppTypeEnum type){
        List<AppEntity> appEntityList = new ArrayList<>();
        List<App> list = dataSource.getInstalledApp(type);
        if (ObjectUtils.isNotEmpty(list)){
            for (App app : list) {
                AppEntity appEntity = new AppEntity();
                BeanUtils.copyProperties(app, appEntity);
                appEntityList.add(appEntity);
            }
        }
        return appEntityList;
    }

    public void downLoadApp(Context context, int action, AppEntity appEntity){
        dataSource.downLoadApp(context, action, appEntity);
    }
}